/*
 * Shooter - attached to a fortressdata to make it attack
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import lib3f.IntPoint;
import lib3f.LCG;
import lib3f.Dist;
#if flash9
	import flash3f.Images;
#end

class Shooter
{
	//static public var shooters : Array <Shooter>; 
	public var weapon : Int;
	public var timetonextshot : Int;
	public var target : IntPoint;
	public var location : IntPoint;
	public var scancache : Array<GameTile>;
	public var fire : Bool;
	public var parent : Dynamic;
	public var shots_hit : Int;
	public var shots_fired : Int;
	public var angle : Float;
	public var dist : Int;
	
	public function new(weapon : Int, parent : Dynamic)
	{
		this.weapon = weapon;
		this.parent = parent;
		Game.cur.shooterman.shooters.push(this);
		target = null;
		timetonextshot = 0;
		location = new IntPoint(parent.tx,parent.ty);
		shots_hit = 0;
		shots_fired = 0;
		angle = 0;
		dist = 5;
	}

	public function upgrade(next : Int)
	{
		this.weapon = next;	
		if (next==2)
			dist = 7;		
	}
	
	public function retarget()
	{
		scancache = new Array();
		if (target == null)
		{
			if (weapon==0)
			{
			var ct : Int = 0;
				for (x in parent.tx-4...parent.tx+5)
				{
					for (y in parent.ty-4...parent.ty+5)
					{
						ct++;
						if (Graph.inbounds(x,y, Game.cur.map) && (Dist.Integer(x, parent.tx, y, parent.ty)<4))
							scancache.push(Game.cur.map[x][y]);
					}
				}
			}
		}
		else
		{
			for (x in target.x-1...target.x+2)
			{
				for (y in target.y-1...target.y+2)
				{
					if (Graph.inbounds(x,y, Game.cur.map))
						scancache.push(Game.cur.map[x][y]);
				}
			}
		}
	}
	
	public function scan()
	{
		if (scancache.length>0)
		{
			if (parent.iscastle)
			{
				if (parent.castlecontrol==true)
				{
					fire = false;
					var best : Int = 0;
					var bestdist : Float = 9999;
					for (t in 0...scancache.length)
					{
						if (scancache[t].creeps.length>0)
						{
							var newdist : Float = Dist.Integer(scancache[t].x,parent.tx,scancache[t].y,parent.ty);
							if (newdist<=bestdist)
							{
								best = t;
								bestdist = newdist;
							}
						}
					}
					if (scancache[best].creeps.length>0)
						shoot(best);
					// castle firing pattern
				}
			}
			else
			{
				var best : Array<Int> = [-1,0];
				for (t in 0...scancache.length)
				{
					if (scancache[t].creeps.length>best[1])
					{
						best = [t, scancache[t].creeps.length];
					}
				}
				if (best[0]>-1)
				{
					shoot(best[0]);
				}
			}
		}
	}
	
	public function shoot(target : Int)
	{
		if (weapon==0)
		{		
			new Projectile(parent.tx, parent.ty, scancache[target].x, scancache[target].y, 34, 1, 5, this);
			timetonextshot = 4;
		}
		else if (weapon == 1)
		{		
			new Projectile(parent.tx, parent.ty, scancache[target].x, scancache[target].y, 34, 0, 5, this);
			timetonextshot = 4;
		}
		else if (weapon==2)
		{
			new Projectile(parent.tx, parent.ty, scancache[target].x, scancache[target].y, 34, 0, 10, this);
			timetonextshot = 4;			
		}
		else if (weapon==3)
		{
			new Projectile(parent.tx, parent.ty, scancache[target].x, scancache[target].y, 34, 2, 8, this);
			timetonextshot = 5;			
		}
		else if (weapon==4)
		{
			new Projectile(parent.tx, parent.ty, scancache[target].x, scancache[target].y, 34, 0, 5, this);
			timetonextshot = 2;			
		}
		shots_fired+=1;
	}
	
	public static function start()
	{
		for (s in Game.cur.shooterman.shooters)
		{
			s.retarget();
			s.timetonextshot = 0;
		}
	}
	
	public static function up()
	{
		for (s in Game.cur.shooterman.shooters)
		{
			if (s.timetonextshot<=0)
			{
				s.scan();
			}
			else
			{
				s.timetonextshot--;
			}
		}
	}
	
	public static function end()
	{
	
	}
	
}
